package dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import dao.DriverDao;
import dao.DriverStateDao;
import dao.OrderDao;
import dao.RowMapper;
import entity.Driver;
import entity.DriverState;
//HanMan
public class DriverDaoImpl implements DriverDao {
	JDBCTemplate jdbc = new JDBCTemplate();
	DriverStateDao ds = new DriverStateDaoImpl();
	OrderDao od = null;
	private class rowMapperImpl implements RowMapper<Driver>{
		@Override
		public Driver rowMapper(ResultSet rs) throws SQLException {
			//driverId, userName, realName, password, phone, driverState, orderList
			DriverState driverstate= new DriverState(rs.getInt("driverStateId"),rs.getString("stateName"),null);
			return new Driver(rs.getInt("driverId"),rs.getString("userName"),
					rs.getString("realName"), rs.getString("password"), rs.getString("phone"),
					driverstate,null);
		}
	}
	@Override
	public Driver queryDriver(String userName, String password) {
		String sql="SELECT d.*,stateName FROM driver d "
				+ " INNER JOIN driverstate ds ON d.`driverStateId`=ds.dsId "
				+ " WHERE d.`userName`=? AND d.`password`=?";
		return jdbc.queryOne(sql,new rowMapperImpl(),userName,password);
	}
   
	@Override
	public Driver queryDriverByDriverId(int driverId) {
		String sql="SELECT d.*,stateName FROM driver d"
				+ " INNER JOIN driverstate ds ON d.`driverStateId`=ds.dsId"
				+ " WHERE d.`driverId`=?";
		return jdbc.queryOne(sql,new rowMapperImpl(),driverId);
	}

	@Override
	public List<Driver> queryDriverByDriStateId(int driStateId) {
		String sql="SELECT d.*,stateName FROM driver d "
				+ " INNER JOIN driverstate ds ON d.`driverStateId`=ds.dsId "
				+ " WHERE d.`driverStateId`=?";
		return jdbc.queryAll(sql,new rowMapperImpl(), driStateId);
	}

	@Override
	public List<Driver> queryAllDriver() {
		String sql="SELECT d.*,stateName FROM driver d "
				+ " INNER JOIN driverstate ds ON d.`driverStateId`=ds.dsId ";
		return jdbc.queryAll(sql,new rowMapperImpl());
	}

}
